<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Pay;
use app\common\model\Banner;
use app\common\model\coupon\Coupon;
use app\common\model\coupon\Log;
use app\common\model\MoneyLog;
use app\common\model\red\Envelope;
use app\common\model\red\Log as RedLog;
use app\common\model\ScoreLog;
use app\common\model\User;
use think\cache\driver\Redis;
use think\Db;

/**
 * 红包接口
 */
class Red extends Api
{
    protected $noNeedLogin = ['lists', 'info'];
    protected $noNeedRight = ['*'];

    /**
     * 红包列表
     *
     */
    public function lists()
    {
        $list = Envelope::all(['status' => 'normal']);
        foreach ($list as &$v) {
            $count = RedLog::where('user_id', $this->auth->id)->where('red_id', $v->id)->count();
            $v['num'] -= $count;
        }
        $this->success('', $list);
    }
    /**
     * 红包预览
     *
     */
    public function info()
    {
        $get = request()->get();
        extract($get);
        $this->success('', Envelope::get($id));
    }
    /**
     * 红包购买
     *
     */
    public function addOrder()
    {
        $param = request()->param();
        extract($param);
        if (!$id) {
            $this->error('参数错误');
        }
        Db::startTrans();
        try {
            $data = Envelope::get($id);
            $data->user_id = $this->auth->id;
            $log = new RedLog;
            $row = $log->addLog($data);
            if ($row && $row->integral > 0) {
                ScoreLog::income($row->user_id, $row->integral, 1, 200,  $row->sn);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('兑换成功', $row);
    }
    /**
     * 红包兑换记录
     *
     */
    public function orderlist()
    {
        $param = request()->param();
        extract($param);
        $list = RedLog::with('envelope')->where('user_id', $this->auth->id)->where('log.status', $status)->paginate(10);
        $this->success('', $list);
    }
    // /**
    //  * 红包支付
    //  *
    //  */
    // public function pay()
    // {
    //     if (!$this->request->isPost()) {
    //         $this->error('提交类型错误');
    //     }
    //     $post = $this->request->post();
    //     extract($post);
    //     if (!isset($id) || empty($id)) {
    //         $this->error('参数错误');
    //     }
    //     $order = RedLog::get($id);
    //     if ($order->state == '1') {
    //         $this->error('订单已兑换');
    //     }
    //     $res = true;
    //     try {
    //         if ($order->integral > 0) {
    //             $res = ScoreLog::income($order->user_id, $order->integral, 1, 200,  $order->sn);
    //         }
    //         $result = $order->save(['state' => 1, 'paytime' => time()]);
    //         if (!$result || !$res)
    //             exception('兑换失败');
    //     } catch (\Exception $e) {
    //         $this->error($e->getMessage());
    //     }
    //     $this->success('兑换成功');
    // }
}
